package com.yhb.model;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.edzh.spring.bootstrap.utils.ObjectMapperFactory;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Plan {
    private String id;
    private String userId;
    private String cardId; // 消费卡号, 对好还业务也是还款卡号
    private String withdrawCardId; // 代付卡号.
    private Long days; // 总执行天数
    private Date endDate;
    private Status status;
    private Date createTime;
    private BigDecimal remaining;
    private String localization;
    private Type type;

    private List<ChannelFlow> flows;

    @JsonSetter("flows")
    public void setFlows(String flows) throws IOException {
        JavaType type = ObjectMapperFactory.getObjectMapper().getTypeFactory().constructParametricType(ArrayList.class, ChannelFlow.class);
        this.flows = ObjectMapperFactory.getObjectMapper().readValue(flows, type);
    }

    public void setFlows(List<ChannelFlow> flows) {
        this.flows = flows;
    }

    //  EXECUTING ---> TERMINATEDs
    //             |-> FINISHED --> FINALIZED
    public enum Status {
        EXECUTING, // 执行中
        TERMINATED, // 已终止
        FINISHED, // 所有消费执行完成
        FINALIZED // 用户确认完成
    }

    public enum Type {
        PAYBACK, // 好还
        CHEAT // 好收
    }
}
